package api

import (
	"github.com/TTvcloud/vcloud-sdk-golang/service/imagex"
	"time"
)

type GetSTSResponse struct {
	SessionToken    string `json:"SessionToken" gorm:"column:SessionToken"`
	AccessKeyId     string `json:"AccessKeyId" gorm:"column:AccessKeyId"`
	SecretAccessKey string `json:"SecretAccessKey" gorm:"column:SecretAccessKey"`
	ExpiredTime     string `json:"ExpiredTime" gorm:"column:ExpiredTime"`
	CurrentTime     string `json:"CurrentTime" gorm:"column:CurrentTime"`
}

func (instance *Instance) GetSTS() (*GetSTSResponse, error) {
	client := imagex.NewInstance()
	client.SetAccessKey(instance.getAccessKey())
	client.SetSecretKey(instance.getSecretKey())
	ret, err := client.GetUploadAuthWithExpire(nil, 24*time.Hour)
	if err != nil {
		return nil, err
	}
	res := GetSTSResponse{
		SessionToken:    ret.SessionToken,
		ExpiredTime:     ret.ExpiredTime,
		CurrentTime:     ret.CurrentTime,
		AccessKeyId:     ret.AccessKeyId,
		SecretAccessKey: ret.SecretAccessKey,
	}
	return &res, nil
}
